using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;


/**
 * 处理ESC按键的监听以及弹出窗口的显示和隐藏。
 */
public class ExitPromptManager : MonoBehaviour
{
    // 用于显示/隐藏退出提示的Panel
    public GameObject exitPanel;

    private bool menuKeys = true;

    // 用于存储ESC按键是否按下
    // private bool escKeyPressed = false;

    void Start()
    {
        // exitPanel.SetActive(false);
    }
    void Update()
    {
        if (menuKeys)
        {
            // 检查ESC按键是否按下
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                exitPanel.SetActive(true);
                // escKeyPressed = true;
                menuKeys = false;
                //暂停
                Time.timeScale = 0;
            }

        }
        else if (Input.GetKeyDown(KeyCode.Escape))
        {
            exitPanel.SetActive(false);
            menuKeys = true;
            //开始
            Time.timeScale = 1;
        }


        // 如果ESC按键按下，并且当前不在UI选择中，则显示退出提示
        // if (escKeyPressed && !EventSystem.current.IsPointerOverGameObject())
        // {
        //     ShowExitPrompt();
        //     escKeyPressed = false;
        // }
    }

    /// <summary>
    /// 显示退出提示
    /// </summary>
    private void ShowExitPrompt()
    {
        exitPanel.SetActive(true);
    }

    /// <summary>
    /// 关闭退出提示
    /// </summary>
    public void QuitGameHideExitPrompt()
    {
        // exitPanel.SetActive(false);
        exitPanel.SetActive(false);
        menuKeys = true;
        //开始
        Time.timeScale = 1;
    }

    /// <summary>
    /// 退出游戏
    /// </summary>
    public void QuitGame()
    {

        exitPanel.SetActive(false);
        menuKeys = true;
        //开始
        Time.timeScale = 1;

        //加载第一个场景
        SceneManager.LoadScene(0);

        // Application.Quit();
    }
}